package org.example.acm;

import java.util.*;

public class GuessNumberSolution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int lineCount = Integer.parseInt(scanner.nextLine());
        Map<String, String> map = new HashMap<>();
        for (int i=0;i<lineCount;i++) {
            String[] line = scanner.nextLine().split(" ");
            map.put(line[0], line[1]);
        }
        String res = getRealNumber(map);
        System.out.println(res);
        scanner.close();
    }

    private static String getRealNumber(Map<String, String> map) {
        //找出不存在的数字
        HashSet<Integer> notExistNumSet = new HashSet<>();
        //找出某个位置不可能出现的数字
        Map<Integer, Set<Integer>> posMap = new HashMap<>();
        for(String k: map.keySet()) {
            String v = map.get(k);
            if (v.equals("0A0B")) {
                for (int i=0;i<k.length();i++) {
                    notExistNumSet.add(Character.getNumericValue(k.charAt(i)));
                }
            } else if(v.startsWith("0A")) {
                for (int i=0;i<k.length();i++) {
                    if (posMap.containsKey(i)) {
                        posMap.get(i).add(Character.getNumericValue(k.charAt(i)));
                    } else {
                        Set<Integer> set = new HashSet<>();
                        set.add(Character.getNumericValue(k.charAt(i)));
                        posMap.put(i, set);
                    }
                }

            }
        }
        List<String> res = new ArrayList<>();
        for (int i=0;i<=9999;i++) {
            String ans = String.format("%04d", i);
            if (checkVaild(ans, map, notExistNumSet, posMap)) {
                res.add(ans);
            }
        }
        if (res.size() == 1) {
            return res.get(0);
        } else {
            return "NA";
        }
    }

    private static boolean checkVaild(String ans, Map<String, String> map, Set<Integer> notExistNumSet, Map<Integer, Set<Integer>> posMap) {
        for (int i=0;i<ans.length();i++) {
            Integer num = Character.getNumericValue(ans.charAt(i));
            if (notExistNumSet.contains(num)) {
                return false;
            }
            if (posMap.get(i).contains(num)) {
                return false;
            }
        }
        for (String guessNum: map.keySet()) {
            if (!getGuessInfo(ans, guessNum).equals(map.get(guessNum))) {
                return false;
            }
        }
        return true;
    }

    private static String getGuessInfo(String ans, String guessNum) {
        int valueAndPosCount = 0;
        int valueCount = 0;
        int[] ansNumCount = new int[10];
        int[] guessNumCount = new int[10];
        for (int i=0;i<ans.length();i++) {
            int a = ans.charAt(i) - '0';
            int b = guessNum.charAt(i) - '0';
            if (a == b) {
                valueAndPosCount++;
            } else {
                ansNumCount[a]++;
                guessNumCount[b]++;
            }
        }
        for (int i=0;i<10;i++) {
            valueCount += Math.min(ansNumCount[i], guessNumCount[i]);
        }
        StringBuilder sb = new StringBuilder();
        sb.append(valueAndPosCount);
        sb.append("A");
        sb.append(valueCount);
        sb.append("B");
        return sb.toString();
    }

    private static String getGuessInfo2(String ans, String guessNum) {
        int valueAndPosCount = 0;
        int valueCount = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i=0;i<ans.length();i++) {
            if (ans.charAt(i) == guessNum.charAt(i)) {
                valueAndPosCount++;
            }
            map.put(ans.charAt(i), map.getOrDefault(ans.charAt(i), 0) + 1);
        }
        for (int i=0;i<ans.length();i++) {
            Character c = guessNum.charAt(i);
            if (map.containsKey(c) && map.get(c) >= 1) {
                valueCount++;
                map.put(c, map.get(c)-1);
            }
        }
        valueCount = valueCount - valueAndPosCount;
        StringBuilder sb = new StringBuilder();
        sb.append(valueAndPosCount);
        sb.append("A");
        sb.append(valueCount);
        sb.append("B");
        return sb.toString();
    }
}
